﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using XQ.WeChatOfficialAccount.BLL.Business;
using XQ.WeChatOfficialAccount.DAL.DAL.Common;
using XQ.WeChatOfficialAccount.DAL.Model;
using XQ.WeChatOfficialAccount.DAL.Model.Business;
using XQ.WeChatOfficialAccount.DAL.Models;
using XQ.WeChatOfficialAccount.WebAPI.Common;
using XQ.WeChatOfficialAccount.WebAPI.Filter;
using XQ.WeChatOfficialAccount.WebAPI.Models;

namespace XQ.WeChatOfficialAccount.WebAPI.Controllers.Business
{
    /// <summary>
    /// 文章操作接口
    /// </summary>
    [FormAuthenticationFilter]
    public class ContextsController : ApiController
    {
        ContextsBLL contextsBLL = new ContextsBLL();

        /// <summary>
        /// 获取文章列表
        /// </summary>
        /// <param name="searchModel"></param>
        /// <returns></returns>
        [Route("api/Contexts/GetList")]
        [HttpGet]
        [AllowAnonymous]
        public ResultModel GetList([FromUri] ContextsSearchModel searchModel)
        {
            ResultModel resultModel = new ResultModel();
            Pagination page = new Pagination();
            searchModel = searchModel == null ? new ContextsSearchModel() : searchModel;
            try
            {
                TokenModel token = TokenHelper.GetTokenByHeaders(Request.Headers);
                resultModel.data = contextsBLL.GetList(searchModel, page, token);
                resultModel.page = page;
                LogBll.Instance.WriteLog(LogBll.LogType.Search, "", token.UID, "获取文章列表", "", "", "", "api/Contexts/GetList：" + JsonConvert.SerializeObject(searchModel));
            }
            catch (Exception ex)
            {
                LogBll.Instance.WriteErrLog("", 0, "获取文章列表报错", "", "", JsonConvert.SerializeObject(searchModel), ex.InnerException != null ? ex.InnerException.Message : ex.Message, "", "Contexts", "GetList");
                resultModel.success = false;
                resultModel.message = ex.Message;
                resultModel.code = "10001";
            }

            return resultModel;
        }

        /// <summary>
        /// 保存文章
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        [Route("api/Contexts/SaveContexts")]
        [HttpPost]
        public ResultModel SaveContexts([FromBody] ContextsSaveModel saveModel)
        {
            ResultModel resultModel = new ResultModel();
            try
            {
                TokenModel token = TokenHelper.GetTokenByHeaders(Request.Headers);
                resultModel.data = contextsBLL.SaveContexts(saveModel, token);
                LogBll.Instance.WriteLog(LogBll.LogType.Search, "", token.UID, "保存文章", "", "", "", "api/Contexts/SaveContexts：" + JsonConvert.SerializeObject(saveModel));
            }
            catch (Exception ex)
            {
                LogBll.Instance.WriteErrLog("", 0, "保存文章报错", "", "", JsonConvert.SerializeObject(saveModel), ex.InnerException != null ? ex.InnerException.Message : ex.Message, "", "Contexts", "SaveContexts");
                resultModel.success = false;
                resultModel.message = ex.Message;
                resultModel.code = "10001";
            }

            return resultModel;
        }

        /// <summary>
        /// 获取文章详情
        /// </summary>
        /// <param name="contextsID"></param>
        /// <returns></returns>
        [Route("api/Contexts/GetDetailByID")]
        [HttpGet]
        [AllowAnonymous]
        public ResultModel GetDetailByID([FromUri] int contextsID)
        {
            ResultModel resultModel = new ResultModel();
            try
            {
                TokenModel token = TokenHelper.GetTokenByHeaders(Request.Headers);
                resultModel.data = contextsBLL.GetDetailByID(contextsID);
                LogBll.Instance.WriteLog(LogBll.LogType.Search, "", token.UID, "获取文章详情", "", "", "", "api/Contexts/获取文章详情：" + contextsID);
            }
            catch (Exception ex)
            {
                LogBll.Instance.WriteErrLog("", 0, "获取文章详情报错", "", "", contextsID.ToString(), ex.Message, "", "Contexts", "获取文章详情");
                resultModel.success = false;
                resultModel.message = ex.Message;
                resultModel.code = "10001";
            }

            return resultModel;
        }

        /// <summary>
        /// 根据分类ID获取文章详情
        /// </summary>
        /// <param name="categoryID"></param>
        /// <returns></returns>
        [Route("api/Contexts/GetDetailByCategoryID")]
        [HttpGet]
        [AllowAnonymous]
        public ResultModel GetDetailByCategoryID([FromUri] int categoryID)
        {
            ResultModel resultModel = new ResultModel();
            try
            {
                TokenModel token = TokenHelper.GetTokenByHeaders(Request.Headers);
                resultModel.data = contextsBLL.GetDetailByCategoryID(categoryID);
                LogBll.Instance.WriteLog(LogBll.LogType.Search, "", token.UID, "根据分类ID获取文章详情", "", "", "", "api/Contexts/GetDetailByCategoryID：" + categoryID);
            }
            catch (Exception ex)
            {
                LogBll.Instance.WriteErrLog("", 0, "根据分类ID获取文章详情报错", "", "", categoryID.ToString(), ex.Message, "", "Contexts", "GetDetailByCategoryID");
                resultModel.success = false;
                resultModel.message = ex.Message;
                resultModel.code = "10001";
            }

            return resultModel;
        }

        /// <summary>
        /// 删除文章
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [Route("api/Contexts/DeleteByIds")]
        [HttpPost]
        public ResultModel DeleteByIds(List<int> ids)
        {
            ResultModel resultModel = new ResultModel();
            try
            {
                TokenModel token = TokenHelper.GetTokenByHeaders(Request.Headers);
                resultModel.data = contextsBLL.DeleteByIds(ids);
                LogBll.Instance.WriteLog(LogBll.LogType.Search, "", token.UID, "删除文章", "", "", "", "api/Contexts/DeleteByIds：" + JsonConvert.SerializeObject(ids));
            }
            catch (Exception ex)
            {
                LogBll.Instance.WriteErrLog("", 0, "删除文章报错", "", "", JsonConvert.SerializeObject(ids), ex.Message, "", "Contexts", "DeleteByIds");
                resultModel.success = false;
                resultModel.message = ex.Message;
                resultModel.code = "10001";
            }

            return resultModel;
        }
    }
}